let BASEURL = "http://manager.qy-zb.com" // 基础地址
// let BASEURL = "http://localhost:8848" // 基础地址

/*
 * 全局请求封装
 * @param path 请求路径
 * @param method 请求类型(GET/POST/DELETE等)
 * @oaram data 请求体数据
 * @param loading 请求未完成是是否显示加载中，默认为true
 */
export default (path, method, data = {}, loading = true) => {
	// 获取存储token
	const token = uni.getStorageSync("Token") || '';
	const userId = uni.getStorageSync('userId') || ''
	if (loading) {
		uni.showLoading({
			title: "加载中",
			mask: true
		});
	};
	// 统一给所有请求里增加userId属性
	//data.userId = userId
	data.token = token
	return tokenRequest(path, method, data, loading, token, userId)
};

// 发送请求函数
function tokenRequest(path, method, data, loading, token, userId) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASEURL + path,
			method: method,
			data,
			header: {
				"Token": token,
				"app_user_id": userId
			},
			success(response) {
				if (response.data === '000600') {
					uni.showToast({
						icon: "none",
						title: '请先登录',
						success: () => {
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/user/Login'
								})
							}, 500)
						}
					});
					return
				}
				if (response.data.code === '000000') {
					resolve(response.data);
				} else {
					uni.showModal({
						title: ' ',
						content: response.data.message,
						showCancel: false,
						buttonText: '确定',
						success: (res) => {
							if (res.confirm) {}
						}
					})
					// uni.showToast({
					// 	icon: "none",
					// 	title: response.data.message
					// });
					//resolve(response.data);
				}
				uni.hideLoading();
			},
			fail(err) {
				uni.showToast({
					icon: "none",
					title: '服务响应失败'
				});
				reject(err);
				uni.hideLoading();
			},
			complete() {
				uni.hideLoading();
			}
		});
	});
}

/**
 * 上传附件
 * @param {*} url 
 * @param {*} relationId : File里关联Id
 * @param {*} business ：File里业务Id
 * @param {*} type : 上传文件类型
 */
export function uploadIcon(url, filePath, formData) {
	console.log(formData)
	return new Promise((resolve, reject) => {
		uni.showLoading()
		uni.uploadFile({
			url: BASEURL + url,
			filePath: filePath,
			name: 'multipartFile',
			formData: formData,
			header: {
				"Content-Type": "multipart/form-data"
			},
			success: function(res) {
				resolve(res.data);
				uni.hideLoading()
			},
			fail: function(err) {
				reject(err);
				uni.hideLoading()
			},
			complete: function(e) {
				uni.hideLoading()
			}
		});
	});
}
